<template>
  <div class="com-container">
    <div class="com-chart"
         ref="stockRef">

    </div>
  </div>
</template>

<script>
import { getStockData } from 'network/stock.js';
export default {
  name: 'Stock',
  data () {
    return {
      echartsInstance: null,
      allData: null, //服务器返回的数据
      currentPage: 1, //当前显示的页数
      totalPage: 1, //总共页数
      timer: null, //定时器标识
    }
  },
  mounted () {
    this.initChart();
    this.getData();
    window.addEventListener('resize', this.adaptScreen);
  },
  destroyed () {
    clearInterval(this.timer);
    window.removeEventListener('resize', this.adaptScreen);
  },
  methods: {
    //初始化echarts实例化对象
    initChart () {
      this.echartsInstance = this.$echarts.init(this.$refs.stockRef, 'chalk');
      //对图表初始化配置的控制
      const initOption = {
        title: {
          text: '▎库存和销量分析',
          left: 20,
          top: 20,
        },
      };
      this.echartsInstance.setOption(initOption);
      //对图表对象进行鼠标事件的监听
      this.echartsInstance.on('mouseover', () => {
        clearInterval(this.timer);
      });
      this.echartsInstance.on('mouseout', () => {
        this.startInterval();
      })
    },

    //获取服务器的数据
    async getData () {
      const { data: res } = await getStockData();
      this.allData = res;
      console.log(this.allData);
      this.totalPage = Math.ceil(this.allData.length / 5);
      this.updateChart();
      this.adaptScreen();
      this.startInterval();
    },

    //更新图表
    updateChart () {
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      const colorArr = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF']
      ]
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const showData = this.allData.slice(start, end);
      const seriesArr = showData.map((item, index) => {
        return {
          type: 'pie',
          center: centerArr[index],
          hoverAnimation: false, //取消鼠标经过的动画
          labelLine: {
            show: false //指示线移出
          },
          label: {
            show: true,
            position: 'center', //标签在饼图中心
            color: colorArr[index][0],
          },
          data: [
            {
              value: item.sales,
              itemStyle: {
                //echart 渐变内置生成器echarts.graphic.LinearGradient
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  //百分之0状态下的颜色值
                  {
                    offset: 0,
                    color: colorArr[index][0]
                  },
                  //百分之100状态下的颜色值
                  {
                    offset: 1,
                    color: colorArr[index][1]
                  }
                ])
              }
            },
            {
              name: item.name + '\n\n' + item.sales, //标签的名字
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }
            }
          ],
        }
      });
      const dataOption = {
        series: seriesArr
      };
      this.echartsInstance.setOption(dataOption);
      this.adaptScreen();

    },

    //浏览器窗口发生变化，调用
    adaptScreen () {
      const titleFontSize = (this.$refs.stockRef.offsetWidth / 100) * 3.6
      // 圆的内院半径和 外圆半径
      const innerRadius = titleFontSize * 2.5
      const outerRadius = innerRadius * 1.2
      let seriesArr = [];
      for (let i = 0; i < 5; i++) {
        seriesArr.push({
          radius: [outerRadius, innerRadius],
          label: {
            fontSize: titleFontSize / 2,
          },
        })
      }
      //和分辨率大小相关的配置项
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: seriesArr
      };
      this.echartsInstance.setOption(adapterOption);
      this.echartsInstance.resize();
    },

    startInterval () {
      if (this.timer) {
        clearInterval(this.timer);
      }
      this.timer = setInterval(() => {
        this.currentPage < this.totalPage ? this.currentPage++ : this.currentPage = 1;
        this.updateChart();
      }, 5000)
    },

  },
}
</script>

<style lang="less" scoped>
</style>